home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / utility / crypchal.zip / CRYPT.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  4KB  |  99 lines

  1. /****************************************************************/
  2. /*                                                              */
  3. /*      S-CODER - Encrypt/decrypt data                          */
  4. /*                                                              */
  5. /*      Copyright 1987-1989 by Robert B. Stout dba MicroFirm    */
  6. /*                                                              */
  7. /*      Originally written by Bob Stout with modifications      */
  8. /*      suggested by Mike Smedley.                              */
  9. /*                                                              */
  10. /*      This code may be used freely in any program for any     */
  11. /*      application, personal or commercial.                    */
  12. /*                                                              */
  13. /*  Current commercial availability:                            */
  14. /*                                                              */
  15. /*      1. MicroFirm Toolkit ver 3.00: LYNX and CRYPT utilities */
  16. /*      2. CXL libraries (MSC, TC, ZTC/C++, PC): fcrypt()       */
  17. /*         dedicated file encryption function                   */
  18. /*      3. SMTC & MFLZT libraries: crypt() function             */
  19. /*                                                              */
  20. /****************************************************************/
  21.  
  22. char *cryptext;         /* The actual encryption/decryption key */
  23. int   crypt_ptr = 0;    /* Circular pointer to elements of key  */
  24. int   crypt_length;     /* Set externally to strlen(cryptext)   */
  25.  
  26. /* NOTES: cryptext should be set and qualified (to something over
  27.           5-6 chars, minimum) by the calling program, which should
  28.           also set crypt_ptr in the range of 0 to strlen(cryptext)
  29.           before each use. If crypt() is used to encrypt several
  30.           buffers, cryptext should be reloaded and crypt_ptr reset
  31.           before each buffer is encrypted. The encryption is both
  32.           reversible - to decrypt data, pass it back through crypt()
  33.           using the original key and original initial value of
  34.           crypt_ptr - and multiple passes are commutative.
  35. */
  36.  
  37. /**** Encrypt/decrypt buffer datum ******************************/
  38. void crypt(unsigned char *buf)
  39. {
  40.       *buf ^= cryptext[crypt_ptr] ^ (cryptext[0] * crypt_ptr);
  41.       cryptext[crypt_ptr] += ((crypt_ptr < (crypt_length - 1)) ?
  42.             cryptext[crypt_ptr + 1] : cryptext[0]);
  43.       if (!cryptext[crypt_ptr])
  44.             cryptext[crypt_ptr] += 1;
  45.       if (++crypt_ptr >= crypt_length)
  46.             crypt_ptr = 0;
  47. }
  48.  
  49. /**** Encrypt/decrypt buffer ************************************/
  50. void bufcrypt(unsigned char *buf, long length)
  51. {
  52.       while (length--)
  53.             crypt(buf++);
  54. }
  55.  
  56. #ifdef TEST
  57.  
  58. #include <stdio.h>
  59. #include <string.h>
  60.  
  61. int main(int argc, char *argv[])
  62. {
  63.       static char buf[16384];
  64.       size_t len, i;
  65.       FILE *in, *out;
  66.  
  67.       if (4 > argc)
  68.       {
  69.             puts("Usage: CRYPT password infile outfile");
  70.             return -1;
  71.       }
  72.       cryptext = argv[1];
  73.       crypt_length = strlen(cryptext);
  74.       if (NULL == (in = fopen(argv[2], "rb")))
  75.       {
  76.             printf("Can't open %s for input\n", argv[2]);
  77.             return -1;
  78.       }
  79.       if (NULL == (out = fopen(argv[3], "wb")))
  80.       {
  81.             printf("Can't open %s for output\n", argv[3]);
  82.             return -1;
  83.       }
  84.       do
  85.       {
  86.             if (0 != (len = fread(buf, 1, 16384, in)))
  87.             {
  88.                   for (i = 0; i < len; ++i)
  89.                         crypt(&buf[i]);
  90.                   fwrite(buf, 1, len, out);
  91.             }
  92.       } while (len);
  93.       fclose(in);
  94.       fclose(out);
  95.       return 0;
  96. }
  97.  
  98. #endif
  99.